namespace Exam1 { internal class Program { class Book { public string Title { get; set; } public string Author { get; set; } public int Year { get; set; } public decimal Price { get; set; } public int Pages { get; set; } public Book(string title, string author, int year, decimal price, int pages) { Title = title; Author = author; Year = year; Price = price; Pages = pages; } public override string ToString() { return $"Title: {Title}, Author: {Author}, Year: {Year}, Price: {Price:C}, Pages: {Pages}"; } } class Library { private List books; public Library() { books = new List(); } public void AddBook(Book book) { books.Add(book); } public void RemoveBook(string title) { books.RemoveAll(b => b.Title.Equals(title, StringComparison.OrdinalIgnoreCase)); } public Book SearchByTitle(string title) { return books.Find(b => b.Title.Equals(title, StringComparison.OrdinalIgnoreCase)); } public List SearchByAuthor(string author) { return books.FindAll(b => b.Author.Equals(author, StringComparison.OrdinalIgnoreCase)); } public void DisplayBooks() { if (books.Count == 0) { Console.WriteLine("No books available."); return; } foreach (var book in books) { Console.WriteLine(book); } } public void SaveToFile(string filename) { using (StreamWriter writer = new StreamWriter(filename)) { foreach (var book in books) { writer.WriteLine($"{book.Title},{book.Author},{book.Year},{book.Price},{book.Pages}"); } } } public void LoadFromFile(string filename) { if (File.Exists(filename)) { books.Clear(); foreach (var line in File.ReadLines(filename)) { var parts = line.Split(','); var book = new Book(parts[0], parts[1], int.Parse(parts[2]), decimal.Parse(parts[3]), int.Parse(parts[4])); books.Add(book); } } } } static void Main(string[] args) { Library library = new Library(); library.LoadFromFile("books.txt"); while (true) { Console.Clear(); Console.WriteLine("Library Management System"); Console.WriteLine("1. Add Book"); Console.WriteLine("2. Remove Book"); Console.WriteLine("3. Search Book by Title"); Console.WriteLine("4. Search Book by Author"); Console.WriteLine("5. Display All Books"); Console.WriteLine("6. Save and Exit"); Console.WriteLine("7. Exit without Saving"); Console.Write("Choose an option: "); string choice = Console.ReadLine(); switch (choice) { case "1": AddBook(library); break; case "2": RemoveBook(library); break; case "3": SearchBookByTitle(library); break; case "4": SearchBookByAuthor(library); break; case "5": library.DisplayBooks(); break; case "6": library.SaveToFile("books.txt"); Console.WriteLine("Books saved. Exiting..."); return; case "7": Console.WriteLine("Exiting without saving."); return; default: Console.WriteLine("Invalid choice. Try again."); break; } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static void AddBook(Library library) { Console.Write("Enter Book Title: "); string title = Console.ReadLine(); Console.Write("Enter Author: "); string author = Console.ReadLine(); Console.Write("Enter Year: "); int year = int.Parse(Console.ReadLine()); Console.Write("Enter Price: "); decimal price = decimal.Parse(Console.ReadLine()); Console.Write("Enter Number of Pages: "); int pages = int.Parse(Console.ReadLine()); library.AddBook(new Book(title, author, year, price, pages)); } static void RemoveBook(Library library) { Console.Write("Enter the title of the book to remove: "); string title = Console.ReadLine(); library.RemoveBook(title); } static void SearchBookByTitle(Library library) { Console.Write("Enter the title to search: "); string title = Console.ReadLine(); var book = library.SearchByTitle(title); if (book != null) { Console.WriteLine(book); } else { Console.WriteLine("Book not found."); } } static void SearchBookByAuthor(Library library) { Console.Write("Enter the author's name to search: "); string author = Console.ReadLine(); var books = library.SearchByAuthor(author); if (books.Count > 0) { foreach (var book in books) { Console.WriteLine(book); } } else { Console.WriteLine("No books found by this author."); } } } } }